package org.snail.attendence.service.impl;

import lombok.extern.log4j.Log4j;
import org.snail.attendence.bean.*;
import org.snail.attendence.mapper.AttendenceMongoMapper;
import org.snail.attendence.service.AttendenceMongoService;
import org.snail.base.AttendenceCondition;
import org.snail.exception.CommonException;
import org.snail.exception.EmpException;
import org.snail.exception.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Log4j
@Service
public class AttendenceMongoServiceImpl implements AttendenceMongoService {

    @Autowired
    private AttendenceMongoMapper attendenceMongoMapper;


    /**
     * 将异常考勤存入mongoDB中
     */
    public int saveAttendenceMongo(Object mongoObject, String mongoName) throws CommonException {
        int result = 0;
        try {
            result = attendenceMongoMapper.saveGeneralAttendence(mongoObject, mongoName);
        } catch (Exception e) {
            log.error("考勤数据保存mongoDB失败,错误原因:" + e.getMessage());
        }

        return result;
    }

    /**
     * 保存考勤列表到mongoDB
     *
     * @param mongoObject
     * @param mongoDBName
     * @return
     * @throws CommonException
     */
    @Override
    public int saveAttendenceList(List<AliWorkDTO> mongoObject, String mongoDBName) throws CommonException {
        int result = 0;
        List<AliWorkDTO> insertList = new ArrayList<>();

        for(AliWorkDTO aliWorkDTO : mongoObject){
            if(aliWorkDTO.getState()==1){
                insertList.add(aliWorkDTO);
            }
        }
        try {
            result = attendenceMongoMapper.saveAttendenceList(insertList, mongoDBName);
        } catch (Exception e) {
            log.error("考勤列表数据保存mongoDB失败,错误原因:" + e.getMessage());
        }
        return result;
    }


    /**
     * 从mongoDB中查询所有考勤信息
     *
     * @return
     */
    public List<AliWorkDTO> findAll() {
        return attendenceMongoMapper.findAll();
    }

    /**
     * 从mongodb中多条件查询List
     *
     * @param aliCondition
     * @return
     */
    public List<AbAttendenceMongo> findAliListByCondition(AttendenceCondition aliCondition) throws CommonException {
        List<AbAttendenceMongo> attendentList = attendenceMongoMapper.findAbAttendenceMgDBList(aliCondition);
        return attendentList;
    }


    /**
     * 强行标记为已读
     *
     * @param
     */
    public void editNormal(AbnormalAttendanceDTO abnormalAttendanceDTO) throws CommonException {
        attendenceMongoMapper.editNormal(abnormalAttendanceDTO);
    }

    /**
     * 查找强行被标记的一些数据
     *
     * @param isCorrect
     * @return
     */
    public List<AbnormalAttendanceDTO> findListByFlag(Integer isCorrect) {
        return attendenceMongoMapper.findListByFlag(isCorrect);
    }

    /**
     * 查找所有含有审批纪录的考勤数据
     *
     * @param aliWorkDTO
     * @return 返回调休单中不为空的数据
     */
    public List<AttendenceMongo> findListContainsApproval(AliWorkDTO aliWorkDTO) {
        return attendenceMongoMapper.findListContainsApproval(aliWorkDTO);
    }

    public void addOrUpDateAttendence(List<AliWorkDTO> aliWorkDTOS) {
        attendenceMongoMapper.addOrUpDateAttendence(aliWorkDTOS);
    }

    /**
     * 保存考勤提示消息到mogoDB
     *
     * @param attendenceMessageList
     * @return
     */
    public JsonResult saveAttendaceMessage(List<AttendenceMessage> attendenceMessageList) {
        JsonResult result = new JsonResult();
        attendenceMongoMapper.saveAttendaceMessage(attendenceMessageList);
        return result;
    }

    public JsonResult findAttendaceMessage() {
        JsonResult result = new JsonResult();
        List<AttendenceMessage> message = attendenceMongoMapper.findAttendaceMessage();
        result.setData(message);
        return result;
    }


    public Integer dropCollection(String collectionName) {
        int res = attendenceMongoMapper.dropCollection(collectionName);
        return res;
    }

    public void updateCorrect(List<AbnormalAttendanceDTO> abnormalAttendanceDTOS) {
        attendenceMongoMapper.updateCorrect(abnormalAttendanceDTOS);
    }

    /**
     * 根据isCorrect查询考勤异常数据  默认为0，表示没有被标记的数据；1表示强行标记的数据；2.表示全部数据
     * 根据传来的groupDOs 查询部门的分组列表
     *
     * @return abnormalAttendanceDTOS 所有的考勤数据
     */
    public List<AbnormalAttendanceDTO> findAbnornalList(AttendenceCondition aliCondition) throws EmpException {
        List<AbnormalAttendanceDTO> abnormalAttendanceDTOS = attendenceMongoMapper.findAbnornalList(aliCondition);
        return abnormalAttendanceDTOS;
    }


}
